package com.lcxw.hxzbapi.controller.goodnamecontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.entity.pojo.GoodNameBuy;
import com.lcxw.hxzbapi.entity.requestBean.BuyGoodNameReq;
import com.lcxw.hxzbapi.entity.pojo.GoodName;
import com.lcxw.hxzbapi.entity.pojo.YwpdMember;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.entity.response.ResponseData;
import com.lcxw.hxzbapi.service.GoodNameBuyService;
import com.lcxw.hxzbapi.service.GoodNameService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbcommon.common.StringUtil;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class GoodNameRealization {

    @Resource
    GoodNameService goodNameService;

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    GoodNameBuyService goodNameBuyService;


    public Response addGoodNames(List<GoodName> goodNames) {

        goodNameService.saveBatch(goodNames);
        return Response.ok();

    }

    public Response getAll(Integer sold, int pageNum, int pageSize) {
        IPage<GoodName> page = new Page<>(pageNum, pageSize);
        QueryWrapper<GoodName> w = new QueryWrapper<>();
        w.eq(sold != null, "sold", sold);
        w.orderByAsc("length");
        w.orderByAsc("name");
        page = goodNameService.page(page, w);
        ResponseData data = new ResponseData();
        data.setTotal(page.getTotal());
        data.setList(page.getRecords());
        return Response.ok(data);
    }

    public Response deleteGoodNames(List<Integer> deleteIdList) {

        goodNameService.removeByIds(deleteIdList);
        return Response.ok();

    }

    Response buyGoodName(BuyGoodNameReq buyGoodNameReq) throws Exception {
        long userId = Long.parseLong(buyGoodNameReq.getUserId());
        int goodNameId = buyGoodNameReq.getId();
        GoodName goodName = goodNameService.getById(goodNameId);
        if (goodName.getSold() == 1) {
            return Response.error(ErrorCode.DATA_NOT_EXIST, "该靓号已经销售");
        }
        YwpdMember ywpdMember = ywpdMemberService.selectYwpdMemberByUserId(userId);
        int price = goodName.getPrice();
        if (ywpdMember.getEarnBean() < price) {
            return Response.error(ErrorCode.NOT_ENOUGH, "幸运币不足，请先购买幸运币");
        }
        ywpdMember.setEarnBean(ywpdMember.getEarnBean() - price);
        ywpdMember.setUserName(goodName.getName());
        ywpdMemberService.updateYwpdMember(ywpdMember);

        goodName.setSold(1);
        goodNameService.updateById(goodName);

        addGoodNameBuy(userId, goodNameId);
        return Response.ok();

    }

    @Async
    void addGoodNameBuy(long userId, int goodNameId) {
        GoodNameBuy goodNameBuy = new GoodNameBuy();
        goodNameBuy.setUserId(userId);
        goodNameBuy.setGoodNameId(goodNameId);
        goodNameBuy.setAddTime(StringUtil.getDateTime());
        goodNameBuyService.save(goodNameBuy);
    }
}
